package com.test.utils.text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class CmdUtils {
	public static void main(String[] args) throws IOException {
		// printList(CmdUtils.getIp("以太网","本地连接"));
		// printList(CmdUtils.checkConnection("baidu.com"));
		// printList(CmdUtils.netList(3306));
		// printList(CmdUtils.taskList("chrome"));
		printList(CmdUtils.killTask("chrome.exe", false));
	}
 
	/**
	 * 根据名称杀死某个进程
	 * @param name 名称
	 * @param isFuzzy 是否模糊,true 模糊匹配 小心使用模糊，会把所有包含name的进程删除
	 */
	public static List<String> killTask(String name, boolean isFuzzy) throws IOException {
		if (!isFuzzy) {
			return killTask(name);
		} else {
			List<String> result = new ArrayList<String>();
			List<String> temp = taskList(name);
			if (temp.size() < 3) {
				return result;
			}
			Set<String> set = new HashSet<String>();
			for (int i = 2; i < temp.size(); i++) {
				set.add(temp.get(i).trim().split(" ")[0]);
			}
			for (String s : set) {
				result.addAll(killTask(s));
			}
			return result;
		}
	}
 
	/**
	 * 打印list
	 */
	public static void printList(List<String> list) {
		for (String s : list) {
			System.out.println(s);
		}
	}
 
	private static List<String> killTask(String name) throws IOException {
		List<String> result = new ArrayList<String>();
		BufferedReader br = getBufferedReaderByCmd("taskkill /f /t /im " + name);
		String line = null;
		while ((line = br.readLine()) != null) {
			result.add(line.trim());
		}
		return result;
	}
 
	/**
	 * 显示任务，可添加条件
	 * @param critters 字符串条件
	 * @throws IOException IO异常 如果不传参数，返回内存使用量前maxNum条的任务
	 *         如传入eclipse则返回eclipse这个任务
	 */
	public static List<String> taskList(String... critters) throws IOException {
		List<String> result = new ArrayList<String>();
		int maxNum = 20;
		BufferedReader br = getBufferedReaderByCmd("tasklist");
		String line = null;
		List<String> list = new ArrayList<String>();
		while ((line = br.readLine()) != null) {
			if (line.contains("映像名称") || line.contains("=========")) {
				result.add(line.trim());
			} else if (checkAllContain(line, critters) && !"".equals(line.trim())) {
				list.add(line.trim());
			}
		}
		list.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				String[] as1 = o1.split(" ");
				String[] as2 = o2.split(" ");
				String s1 = as1[as1.length - 2].replaceAll(",", "");
				String s2 = as2[as2.length - 2].replaceAll(",", "");
				return Integer.parseInt(s2) - Integer.parseInt(s1);
			}
		});
		if (list.size() < 10) {
			maxNum = list.size();
		}
		for (int i = 0; i < maxNum; i++) {
			result.add(list.get(i));
		}
		return result;
	}
 
	/**
	 * 查询活动链接
	 */
	public static void netList() throws IOException {
		netList(-1);
	}
 
	/**
	 * 查询某个接口的活动链接
	 * @param port 接口
	 */
	public static List<String> netList(Integer port) throws IOException {
		String[] critters = { "" };
		if (port != -1) {
			critters[0] = port + "";
		}
		List<String> result = new ArrayList<String>();
		BufferedReader br = getBufferedReaderByCmd("netstat -ano");
		String line = null;
		while ((line = br.readLine()) != null) {
			if (line.contains("PID") || checkAllContain(line, critters)) {
				result.add(line.trim());
			}
		}
		return result;
	}
 
	/**
	 * 判断链接是否能连接上
	 * @param url 链接地址
	 */
	public static List<String> checkConnection(String url) throws IOException {
		List<String> result = new ArrayList<String>();
		BufferedReader br = getBufferedReaderByCmd("ping " + url);
		String line = null;
		while ((line = br.readLine()) != null) {
			result.add(line.trim());
		}
		return result;
	}
 
	private static BufferedReader getBufferedReaderByCmd(String cmd) throws IOException {
		String[] encodings = { "GBK", "ISO-8859-1", "utf-8" };
		Process p = Runtime.getRuntime().exec(cmd);
		return new BufferedReader(new InputStreamReader(p.getInputStream(), encodings[0]));
	}
 
	/**
	 * 获取某网的IP和物理地址
	 * @param args 条件 如输入"以太网","本地连接"查询
	 */
	public static List<String> getIp(String... args) throws IOException {
		List<String> result = new ArrayList<String>();
		String[] critters = { "IPv4", "物理地址" };
		BufferedReader br = getBufferedReaderByCmd("ipconfig /all");
		String line = null;
		boolean check = false;
		while ((line = br.readLine()) != null) {
			if (line.contains("适配器")) {
				if (checkAllContain(line, args)) {
					check = true;
					result.add(line.trim());
				} else {
					check = false;
				}
			}
			if (check) {
				if (checkOneContain(line, critters)) {
					result.add(line.trim());
				}
			}
		}
		return result;
	}
 
	private static boolean checkOneContain(String line, String... args) {
		for (String s : args) {
			if (line.contains(s)) {
				return true;
			}
		}
		return false;
	}
 
	private static boolean checkAllContain(String line, String... args) {
		for (String s : args) {
			if (!line.contains(s)) {
				return false;
			}
		}
		return true;
	}
}